package com.choudou5.solr.utils;

import cn.hutool.core.util.EscapeUtil;
import cn.hutool.crypto.SecureUtil;

/**
 * @Name：密码 工具类
 * @@author choudou5
 * @@date 2018-07-14
 */
public class PasswordUtil {

    /**
     * 生成 盐值
     */
    private static String genSalt(Integer id) {
        String salt = SecureUtil.md5(id.toString()).substring(0, 15);
        return salt;
    }

    /**
     * 生成安全的密码
     */
    public static String entrypt(Integer id, String plainPassword) {
        String plain = EscapeUtil.unescape(plainPassword);
        String hashPassword = SecureUtil.sha1(plain);
        return genSalt(id)+SecureUtil.sha1(hashPassword);
    }


    /**
     * 验证密码
     * @param dbPwd 密文密码
     * @param pwd 明文密码
     * @return 验证成功返回true
     */
    public static boolean validate(Integer id, String dbPwd, String pwd) {
        return dbPwd.equals(entrypt(id, pwd));
    }

    /* ================= test ================= */
    public static void main(String[] args) {
        Integer id = 1;
        System.out.println(entrypt(id, "solradmins"));
        System.out.println(validate(id, "c4ca4238a0b923872b219a17f5d64662d1e1c80c487e9c858eaa4a7", "solradmins"));
    }

}
